const App = getApp();
const wxParse = require('../../wxParse/wxParse.js')
const qingxilist = require('../../utils/qingxilist.js')
const siteinfo = require('../../siteinfo.js')

Page({

  data: {
    // 页面参数
    qingxilist:qingxilist,
    options: {},
    // 页面元素
    scrollTop: 0,
    index:0,
    detail: {},
    IsGongshui: '有',
    tuResult:true,//检测一下是不是有没有图片未传完
    XianChangZhaoPian:[],
    GongZuoZhaoPian:[],
    WenTiZhaoPian:[],
    qingkong:'',
    ErGongShuiChiShuiZhiJianCeShiJian: '',
    TianBiaoRiQi: '',
    ErGongSheShiQingXiShiJian: '',
    GuanWangShiYongShiJian: '',
    ErGongSheShiJianCeShiJian: '',
    ErGongJiaYaSheBeiTiaoYaShiJian:'',
    YouZhiYongShuiShiJian:'',
    ErGongSheShiQingXiJiGou:'',
    ErGongXingShi: '', //二供形式，用于检测是否无负压供水
    LvHuaYongShui: '', //绿化水，用户检测是否中水
    // XiaoQuleiXing:'',//小区类型
    $data:{},
    qingkongList:[], //搜索的机构的list
    XiaoQuleiXingList: [{
        value: '居民'
      },
      {
        value: '办公'
      },
      {
        value: '行政'
      },
      {
        value: '酒店宾馆'
      },
      {
        value: '医院'
      },
      {
        value: '学校'
      },
      {
        value: '商场'
      },
      {
        value: '居民集资房'
      },
      {
        value: '居民自建房'
      },
      {
        value: '统建楼'
      },
      {
        value: '商住一体'
      },
      {
        value: '其它'
      }
    ],
    LouCengGaoDuList: [{
        value: '平层（1-3层）'
      },
      {
        value: '多层（4-6层）'
      },
      {
        value: '小高层（7-10层）'
      },
      {
        value: '高层（11-30层）'
      },
      {
        value: '超高层（30层以上）'
      },
    ],
    guancaiList: [{
        value: '灰口铸铁管'
      },
      {
        value: '镀锌钢管'
      },
      {
        value: '钢管'
      },
      {
        value: 'PVC-U'
      },
      {
        value: 'PPR'
      },
      {
        value: 'PE'
      },
      {
        value: '不锈钢'
      }
    ],
    gongshuiList: [{
        value: '已立项：改造完成'
      },
      {
        value: '已立项：改造中'
      },
      {
        value: '已立项：暂未实施'
      },
      {
        value: '未立项：物业管理单位无改造意愿'
      },
      {
        value: '未立项：物业管理单位有改造意愿'
      }
    ],
    yiyuanList: [{
      value: '已立项：改造完成'
    },
    {
      value: '已立项：改造中'
    },
    {
      value: '已立项：暂未实施'
    },
    {
      value: '未立项：物业管理单位无改造意愿'
    },
    {
      value: '未立项：物业管理单位有改造意愿'
    }
    ],
    weiyiyuanList: [{
        value: '未纳入改造'
      },
      {
        value: '物业管理单位无改造意愿'
      },
    ],
    isYouwuList: [{
        value: '有'
      },
      {
        value: '无'
      },
    ],
    isShifouList: [{
        value: '是'
      },
      {
        value: '否'
      },
    ],
    ErGongXingShiList: [{
        value: '管道泵直抽供水'
      },
      {
        value: '无负压供水'
      },
      {
        value: '生活水池+水泵'
      },
      {
        value: '生活水池+水泵+天面池'
      },
      {
        value: '生活水池+水泵+天面池+水泵'
      },
      {
        value: '其它'
      },
    ],
    ErGongXiaoFangJieHeList: [{
        value: '生活水池与消防水池共用'
      },
      {
        value: '生活水池与消防水池独立'
      },
      {
        value: '其它'
      },
    ],
    ErGongDiXiaShuiChiCaiZhiList: [{
        value: '瓷砖内饰面'
      },
      {
        value: '不锈钢'
      },
      {
        value: '其它'
      },
    ],
    ErGongShuiChiList: [{
        value: '渗漏'
      },
      {
        value: '溢流'
      },
      {
        value: '池壁卫生不良'
      },
    ],
    ErGongJiaYaSheBeiList: [{
        value: '管道泵'
      },
      {
        value: '恒压（工频）泵'
      },
      {
        value: '变频泵'
      },
      {
        value: '其它'
      }
    ],
    ShuiBengGuoLiuCaiZhiList: [{
        value: '不锈钢'
      },
      {
        value: '铸铁'
      },
      {
        value: '其它'
      },
    ],
    YuanChengGuanLiList: [{
        value: '实现远程智能监控管理'
      },
      {
        value: '未实现远程智能监控管理'
      },
    ],
    RenYuanPeiBeiList: [{
        value: '专人'
      },
      {
        value: '兼职'
      },
    ],
    LvHuaYongShuiList: [{
        value: '自来水'
      },
      {
        value: '中水'
      },
      {
        value: '其它'
      }
    ],
    GuanGaiMoShiList: [{
        value: '滴灌'
      },
      {
        value: '喷灌'
      },
      {
        value: '漫灌'
      },
      {
        value: '其它'
      }
    ],
    jigouList: [{
        value: '物业管理单位'
      },
      {
        value: '深圳水务集团福田分公司'
      }
    ],
    danweiList: [{
        value: '物业管理单位'
      },
      {
        value: '其它'
      }
    ],
    YiLiuGuanJuTiList: [{
        value: '空气隔断'
      },
      {
        value: '倒流防止器          '
      }
    ],
    FenQuXingShiList: [{
        value: '高、低两区'
      },
      {
        value: '高、中、低三区'
      }
    ],
    JinChuShuiGuanCaiZhiList: [{
        value: '镀锌钢管'
      },
      {
        value: '不锈钢管'
      }, {
        value: '钢塑复合管'
      },
      {
        value: '其它'
      }
    ],
    ZhiNengGuanLiList:[
      {
        value: '实现远程智能自动化管理'
      },
      {
        value: '未实现远程智能自动化管理'
      }
    ],
    addr: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    let arr = {..._this.data}
    delete arr.$data
    this.setData({
      $data:arr
    })

  },

  getDetail(e) {
    let _this = this
    App._get("GetShiYaoProjectInfo", {
      id: e,
      userId: wx.getStorageSync('user_id') ? wx.getStorageSync('user_id') : ''
    }, (res) => {
      console.log(res)
      _this.setData({
        detail: res
      })
      wxParse.wxParse('article', 'html', res.YaoQiu, _this, 0)
    })
  },

  radioChange(e) {},
  radioChangeErGongXingShi(e) {
    this.setData({
      ErGongXingShi: e.detail.value
    })
  },
  radioChangeIsGongshui(e) {
    this.setData({
      IsGongshui: e.detail.value
    })
  },
  radioChangeLvHuaYongShui(e) {
    this.setData({
      LvHuaYongShui: e.detail.value
    })
  },
  radioChangeXiaoQuleiXing(e) {
    this.setData({
      XiaoQuleiXing: e.detail.value
    })
  },
  radioChangeErGongXiaoFangJieHe(e) {
    console.log(e.detail.value)
    this.setData({
      ErGongXiaoFangJieHe: e.detail.value
    })
  },
  radioChangeErGongDiXiaShuiChiCaiZhi(e) {
    console.log(e.detail.value)
    this.setData({
      ErGongDiXiaShuiChiCaiZhi: e.detail.value
    })
  },
  radioChangeErGongJiaYaSheBei(e) {
    console.log(e.detail.value)
    this.setData({
      ErGongJiaYaSheBei: e.detail.value
    })
  },
  radioChangeShuiBengGuoLiuCaiZhi(e) {
    console.log(e.detail.value)
    this.setData({
      ShuiBengGuoLiuCaiZhi: e.detail.value
    })
  },

  selectAddress() {
    App.getPermission(this)
  },
  formSubmit(e) {
    var _this = this
    let data = {
      ...e.detail.value
    }
    try{
    if(_this.data.loading){
      return
    }

    if(!_this.data.tuResult){
      wx.showModal({
        cancelColor: 'cancelColor',
        showCancel:false,
        title:'',
        content:'图片上传中，请稍后提交'
      })
      return
    }

    if(!data.ErGongSheShiQingXiJiGou&&data.ErCiGongShui == '有'){
      wx.showModal({
        cancelColor: 'cancelColor',
        showCancel:false,
        title:'提示',
        content:'二供设施清洗机构没有选择'
      })
      wx.pageScrollTo({
        scrollTop: 6000,
        duration:300
      });
      return
    }
    _this.setData({
      loading:true
    })
      data.ErGongShuiChiShuiZhiJianCeShiJian = this.data.ErGongShuiChiShuiZhiJianCeShiJian
      data.RiQi = this.data.RiQi
      data.ErGongSheShiQingXiShiJian = this.data.ErGongSheShiQingXiShiJian
      data.GuanWangShiYongShiJian = this.data.GuanWangShiYongShiJian
      data.ErGongSheShiJianCeShiJian = this.data.ErGongSheShiJianCeShiJian
      data.YouZhiYongShuiShiJian = this.data.YouZhiYongShuiShiJian
      data.ErGongJiaYaSheBeiTiaoYaShiJian = this.data.ErGongJiaYaSheBeiTiaoYaShiJian
      data.XianChangZhaoPian = this.data.XianChangZhaoPian.join(",")
      data.GongZuoZhaoPian = this.data.GongZuoZhaoPian.join(",")
      data.WenTiZhaoPian = this.data.WenTiZhaoPian.join(",")
    }

    catch(e){
      wx.showModal({
        cancelColor: 'cancelColor',
        showCancel:false,
        title:'错误',
        content:'e'
      })
    }

    // for(let i in data){
    //   if(data[i] === ''){
    //     wx.showToast({
    //       title: '有选项未填写',
    //     })
    //     return
    //   }
    // }
    // data.SYId = this.data.SYId
    wx.showLoading({
      title: '提交中',
    })
    App._post_form(e.detail.target.dataset.xingwei == "tijiao"?'ShuiChangShenHe':'SaveShuiChangXunCha', data, (res) => {
      this.setData(_this.data.$data)
      wx.showModal({
        title: res,
        showCancel: false,
        complete: () => {
          // wx.navigateBack()
          _this.setData({
            loading:false
          })
        }
      })
    },()=>{},()=>{
      wx.hideLoading()
    })
  },

  /**
   * 分享当前页面
   */
  onShareAppMessage() {
    let _this = this;
    return {
      title: _this.data.detail.Name,
      path: `/pages/xiaoqu/idnex`,
    };
  },
  bindDateChange(e) {

    this.setData({
      RiQi: e.detail.value
    })
  },
  blurLianXiFangShi(e) {
    if (e.detail.value.length != "11") {
      wx.showModal({
        showCancel: false,
        title: '手机号必须为11位'
      })
    }
  },
  blurZuoji(e) {
    if (e.detail.value.length != "7" && e.detail.value.length != "8") {
      wx.showModal({
        showCancel: false,
        title: '座机号码只能为7位或者8位'
      })
    }
  },
  bindXianChangZhaoPian(){
    let _this =this
    var XianChangZhaoPianList = []
    wx.chooseMedia({
      count: 9,
      mediaType: ['image','video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        let arr = [];
        _this.setData({
          tuResult:false
        })
        for(let i in res.tempFiles){
         let promise = new Promise((rel,rej)=>{
          App.uploaFile('/api/UpLoadProcess',res.tempFiles[i].tempFilePath,(res2)=>{
            rel(siteinfo.siteroot+res2)
          })
         })
         arr.push(promise)
        }
        Promise.all(arr).then((result)=>{
          console.log(result,'图片上传完成')
          let arrList = []
          arrList = arrList.concat(_this.data.XianChangZhaoPian)
          arrList.push.apply(arrList,result)
          _this.setData({
            XianChangZhaoPian:arrList,
            tuResult:true
          })
        })
      }
    })
  },
  searchQingkong(e){
    let _this = this;
    App._get('GetQingXiJiGou',{name:e.detail.value},(res)=>{
      console.log(res)
      if(res.length === 0){
        res = [{Name:'暂无数据'}]
      }
      _this.setData({
        qingkongList:res
      })
    })
  },
  selectQingkong(e){
    this.setData({
      qingkongList:[],
      ErGongSheShiQingXiJiGou:e.currentTarget.dataset.name
    })
  },
  bindGongZuoZhaoPian(){
    let _this =this
    var GongZuoZhaoPianList = []
    wx.chooseMedia({
      count: 9,
      mediaType: ['image','video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        let arr = [];
        _this.setData({
          tuResult:false
        })
        for(let i in res.tempFiles){
         let promise = new Promise((rel,rej)=>{
          App.uploaFile('/api/UpLoadProcess',res.tempFiles[i].tempFilePath,(res2)=>{
            rel(siteinfo.siteroot+res2)
          })
         })
         arr.push(promise)
        }
        Promise.all(arr).then((result)=>{
          console.log(result,'图片上传完成')
          let arrList = []
          arrList = arrList.concat(_this.data.GongZuoZhaoPian)
          arrList.push.apply(arrList,result)
          _this.setData({
            GongZuoZhaoPian:arrList,
            tuResult:true
          })
        })
      }
    })
  },
  bindWenTiZhaoPian(){
    let _this =this
    var WenTiZhaoPianList = []
    wx.chooseMedia({
      count: 9,
      mediaType: ['image','video'],
      sourceType: ['album', 'camera'],
      maxDuration: 30,
      camera: 'back',
      success(res) {
        let arr = [];
        _this.setData({
          tuResult:false
        })
        for(let i in res.tempFiles){
         let promise = new Promise((rel,rej)=>{
          App.uploaFile('/api/UpLoadProcess',res.tempFiles[i].tempFilePath,(res2)=>{
            rel(siteinfo.siteroot+res2)
          })
         })
         arr.push(promise)
        }
        Promise.all(arr).then((result)=>{
          console.log(result,'图片上传完成')
          let arrList = []
          arrList = arrList.concat(_this.data.WenTiZhaoPian)
          arrList.push.apply(arrList,result)
          _this.setData({
            WenTiZhaoPian:arrList,
            tuResult:true
          })
        })
      }
    })
  },
  shanchu(e){
  //  e.target.name
  let _this = this 
   console.log(e)
   let arr = []
   arr = arr.concat(_this.data[e.target.dataset.name])
   arr.splice(e.target.dataset.index,1)
   this.setData(_this.setKeyAndValue(e.target.dataset.name,arr))
  },
  setKeyAndValue(key,value){
    var obj = {};
    console.log(key)
    obj[key]=value
    console.log(obj)
    return obj
    },
    bindXianChangZhaoPian(){
      let _this =this
      var XianChangZhaoPianList = []
      wx.chooseMedia({
        count: 9,
        mediaType: ['image','video'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
          let arr = [];
          _this.setData({
            tuResult:false
          })
          for(let i in res.tempFiles){
           let promise = new Promise((rel,rej)=>{
            App.uploaFile('/api/UpLoadProcess',res.tempFiles[i].tempFilePath,(res2)=>{
              rel(siteinfo.siteroot+res2)
            })
           })
           arr.push(promise)
          }
          Promise.all(arr).then((result)=>{
            console.log(result,'图片上传完成')
            let arrList = []
            arrList = arrList.concat(_this.data.XianChangZhaoPian)
            arrList.push.apply(arrList,result)
            _this.setData({
              XianChangZhaoPian:arrList,
              tuResult:true
            })
          })
        }
      })
    },
    searchQingkong(e){
      let _this = this;
      App._get('GetQingXiJiGou',{name:e.detail.value},(res)=>{
        console.log(res)
        if(res.length === 0){
          res = [{Name:'暂无数据'}]
        }
        _this.setData({
          qingkongList:res
        })
      })
    },
    selectQingkong(e){
      this.setData({
        qingkongList:[],
        ErGongSheShiQingXiJiGou:e.currentTarget.dataset.name
      })
    },
    bindGongZuoZhaoPian(){
      let _this =this
      var GongZuoZhaoPianList = []
      wx.chooseMedia({
        count: 9,
        mediaType: ['image','video'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
          let arr = [];
          _this.setData({
            tuResult:false
          })
          for(let i in res.tempFiles){
           let promise = new Promise((rel,rej)=>{
            App.uploaFile('/api/UpLoadProcess',res.tempFiles[i].tempFilePath,(res2)=>{
              rel(siteinfo.siteroot+res2)
            })
           })
           arr.push(promise)
          }
          Promise.all(arr).then((result)=>{
            console.log(result,'图片上传完成')
            let arrList = []
            arrList = arrList.concat(_this.data.GongZuoZhaoPian)
            arrList.push.apply(arrList,result)
            _this.setData({
              GongZuoZhaoPian:arrList,
              tuResult:true
            })
          })
        }
      })
    },
    bindWenTiZhaoPian(){
      let _this =this
      var WenTiZhaoPianList = []
      wx.chooseMedia({
        count: 9,
        mediaType: ['image','video'],
        sourceType: ['album', 'camera'],
        maxDuration: 30,
        camera: 'back',
        success(res) {
          let arr = [];
          _this.setData({
            tuResult:false
          })
          for(let i in res.tempFiles){
           let promise = new Promise((rel,rej)=>{
            App.uploaFile('/api/UpLoadProcess',res.tempFiles[i].tempFilePath,(res2)=>{
              rel(siteinfo.siteroot+res2)
            })
           })
           arr.push(promise)
          }
          Promise.all(arr).then((result)=>{
            console.log(result,'图片上传完成')
            let arrList = []
            arrList = arrList.concat(_this.data.WenTiZhaoPian)
            arrList.push.apply(arrList,result)
            _this.setData({
              WenTiZhaoPian:arrList,
              tuResult:true
            })
          })
        }
      })
    },
  /**
   * 下拉刷新
   */
  onPullDownRefresh: function () {
    // 获取首页数据
    wx.stopPullDownRefresh();
  },
    /**
   * 滚动到底部
   */
  goBottom(){
    var obj=wx.createSelectorQuery();
    obj.selectAll('.container').boundingClientRect(function (rect) {
      wx.pageScrollTo({
        scrollTop: rect[0].height,
        duration:300
      });
  })
  obj.exec() ;

  },
  onShareAppMessage() {
    let _this = this;
    return {
      title: '水厂巡查',
      path: "/pages/xiaoqu/index"
    };
  },

});